package gameEngine.com.state;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.KeyEvent;
import gameEngine.com.*;
import gameEngine.com.Actor.Actor;
import gameEngine.com.Actor.Bullet;

public class StateLoading extends MainActivity {

	public static int loadingtype = -1;
	public static String levelStr = "level/level";
	public static final int LOADING_TYPE_TO_MAINMENU = 0;
	public static final int LOADING_TYPE_GAME_TO_GAME = 1;
	public static final int LOADING_TYPE_GAME_TO_GAME_STEPS = 20;
	public static int loadingcurrentStep = 0;
	public static int step = 0;
	public static int LOADING_BAR_X = 150;
	public static int LOADING_BAR_Y = 400;
	public static int LOADING_BAR_W = SCREEN_WIDTH - 2*LOADING_BAR_X;
	public static int LOADING_BAR_H = 18;

	public static synchronized void SendMessageStateLoading(int type) {

		switch (type) {
		case MESSAGE_CTOR:
			step = 0;
			break;
		case MESSAGE_UPDATE:
			step++;
			switch (loadingtype) {
			case LOADING_TYPE_TO_MAINMENU:

				break;
			case LOADING_TYPE_GAME_TO_GAME:
				switch (step) {
				case 1: // clean all data
					GameLayer.arrayListBitmap = null;
					GameLayer.arrayListDataMap = null;
					GameLayer.arrayListAObject = null;
					StateGameplay.listBullet.clear();
					StateGameplay.listTankEmeny.clear();
					StateGameplay.tankMC = null;
					GameLayer.tileMap = null;
					break;
				case 2:// load map
					try {
						Log.d("Loading level : ", " " + levelStr + StateGameplay.level + ".tmx");
						GameLayer.loadLayerBG(levelStr + StateGameplay.level + ".tmx");
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				case 3: // loading sprite common
					if (StateGameplay.spriteHud == null)
						StateGameplay.spriteHud = new Sprite("sprite/ui/hud.spr", true);
					if (Bullet.publicSprite == null)
						Bullet.publicSprite = new Sprite("sprite/actor/bullet.sprt");
					break;
				default:
					break;
				}

				Log.d("Loading level : ", " " + step);
				if (step >= LOADING_TYPE_GAME_TO_GAME_STEPS + 1)
					changeState(STATE_GAMEPLAY);
				break;

			default:
				break;
			}
			break;
		case MESSAGE_PAINT:
			float temp = (LOADING_BAR_W - 4) / LOADING_TYPE_GAME_TO_GAME_STEPS;
			temp = step * temp;
			mainPaint.setStyle(Style.FILL);
			mainPaint.setColor(Color.WHITE);
			mainCanvas.drawRect(LOADING_BAR_X, LOADING_BAR_Y, LOADING_BAR_X + LOADING_BAR_W, LOADING_BAR_Y + LOADING_BAR_H, mainPaint);
			mainPaint.setColor(Color.RED);
			mainCanvas.drawRect(LOADING_BAR_X + 2, LOADING_BAR_Y + 2, LOADING_BAR_X + temp, LOADING_BAR_Y + LOADING_BAR_H - 2, mainPaint);
			fontsmall.drawString(mainCanvas, "Loading", 300, 300, 2);
			break;
		case MESSAGE_DTOR:

			break;
		}
	}

	public static synchronized void update(int type) {

	}
}
